"""
  Copyright 2008 Texas A&M University Licensed under the
  Educational Community License, Version 2.0 (the "License"); you may
  not use this file except in compliance with the License. You may
  obtain a copy of the License at
  
  http://www.osedu.org/licenses/ECL-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an "AS IS"
  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
"""
import unittest

import zope.interface

from lxml import etree

from meercat.base import Metadata
from meercat.mapreduce import interfaces
from meercat.mapreduce.reducers import oai



test_data = {}
test_data['one'] = '''
<oai_dc:dc 
  xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ 
  http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:title>Title 1</dc:title> 
    <dc:creator>Guy, Weird S.</dc:creator>
    <dc:subject>Digital Libraries</dc:subject> 
    <dc:date>2001-12-14</dc:date>
</oai_dc:dc>'''

test_data['two'] = '''
<oai_dc:dc 
  xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ 
  http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:title>Title 1</dc:title> 
    <dc:creator>Guy, Weird Smelling</dc:creator>
    <dc:subject>Digital Libraries</dc:subject> 
    <dc:date>2002-12-14</dc:date>
</oai_dc:dc>'''

test_data['output'] = '''
<oai_dc:dc 
  xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ 
  http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:title>Title 1</dc:title> 
    <dc:creator>Guy, Weird S.</dc:creator>
    <dc:subject>Digital Libraries</dc:subject> 
    <dc:date>2001-12-14</dc:date>
    <dc:title>Title 1</dc:title> 
    <dc:creator>Guy, Weird Smelling</dc:creator>
    <dc:subject>Digital Libraries</dc:subject> 
    <dc:date>2002-12-14</dc:date>
</oai_dc:dc>'''

class BaseTest(unittest.TestCase):
    def setUp(self):
        self.reducer = oai.OaiDcReducer()
    
    def testFormats(self):
        self.assertEqual(list(self.reducer.getFormats()),['oai_dc'])
    
    def testReductions(self):
        result = self.reducer.reduce(Metadata(format='oai_dc',
                                              source='test',
                                              value=test_data['one']),
                                     Metadata(format='oai_dc',
                                              source='test',
                                              value=test_data['two']))
        
        self.assertEqual(etree.fromstring(result.getValue()),
                         etree.fromstring(test_data['output']))
    

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(BaseTest))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())